import { UserAddState } from '@/pages/personnelEntry/components/AddPerson/interface';
import { UserModifyState } from '@/pages/personnelEntry/components/ModifyPerson/interface';
import {
    PersonnelEntryState,
    UserCateInfo,
} from '@/pages/personnelEntry/interface';
import request from './index';

interface FetchListParams {
    page: number;
    limit: number;
}

export function fetchUserList(data: FetchListParams): Promise<RDD<any>> {
    return request.post('/getUserInfo', {
        data,
    });
}

export function deleteUser(id: string | string[]): Promise<RD> {
    const data = Array.isArray(id)
        ? { user_ids: JSON.stringify(id) }
        : { user_id: id };
    return request.post('/userDelete', {
        data: {
            ...data,
            check: true,
        },
    });
}

export function batchEditUser(datas: string): Promise<RD> {
    return request.post('/userBatchEdit', {
        data: {
            userInfo: datas,
        },
    });
}

export function editUser(data: UserModifyState): Promise<RD> {
    return request.post('/userEdit', {
        data: data,
    });
}

export function batchAddUser(datas: UserAddState[]): Promise<RD> {
    return request.post('/userBatchAdd', {
        data: {
            userInfo: JSON.stringify(datas),
        },
    });
}

export function addUser(data: UserAddState): Promise<RD> {
    return request.post('/userAdd', {
        data,
    });
}

export function fetchUserDetail(
    id: PersonnelEntryState['id'],
): Promise<RDD<PersonnelEntryState>> {
    return request.post('/getUserDetail', {
        params: {
            user_id: id,
        },
    });
}

export function fetchUserCateInfo(): Promise<RDD<UserCateInfo>> {
    return request.post('/userCateInfo', {
        params: {
            encode: true,
        },
    });
}
// avatar表示单用户头像，book表示书籍封面，user表示用户批量上传excel文件（暂只支持xlsx文件），books表示书籍批量上传excel文件（暂只支持xlsx文件）
export function uploadFile(data: FormData): Promise<RDD<{ url: string }>> {
    return request.post('/uploadFile', {
        data: data,
    });
}

export function uploadMultipleImport(file: File) {
    const formdata = new FormData();
    formdata.append('field', 'user');
    formdata.append('user', file);
    return uploadFile(formdata);
}
